package echat.future.cn.echat.home

import android.content.Intent
import android.os.Bundle
import android.preference.PreferenceManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import com.zhusx.core.adapter._BaseAdapter
import com.zhusx.core.app._BaseAppCompatActivity
import com.zhusx.core.interfaces.IHttpResult
import com.zhusx.core.network.HttpRequest
import echat.future.cn.echat.R
import echat.future.cn.echat.home.bean.Apperance
import echat.future.cn.echat.home.network.LoadData
import echat.future.cn.echat.home.network.SimpleHttpListener
import kotlinx.android.synthetic.main.activity_search.*

/**
 *
 * Author  zhusx
 * Email   327270607@qq.com
 * Create  2018/8/9 20:11
 */
class SearchActivity : _BaseAppCompatActivity() {
    lateinit var set: MutableSet<String>
    lateinit var adapter: _BaseAdapter<String>
    lateinit var hotAdapter: _BaseAdapter<Apperance>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search)
        initView()
        initRequest()
    }

    private fun initRequest() {
        LoadData<List<Apperance>>(LoadData.Api.形象标签列表, this)
                .apply {
                    _setOnLoadingListener(object : SimpleHttpListener<List<Apperance>>() {
                        override fun onLoadComplete(api: LoadData.Api, request: HttpRequest<Any>, result: IHttpResult<List<Apperance>>) {
                            if (result.data.isNotEmpty()) {
                                tv_hot.visibility = View.VISIBLE
                                hotAdapter._setItemToUpdate(result.data)
                            }
                        }
                    })
                }._loadData()
    }

    private fun initView() {
        iv_return.setOnClickListener { finish() }
        iv_delete.setOnClickListener {
            set.clear()
            adapter._clearItemToUpdate()
            _showToast("已清空")
            layout_search.visibility = View.GONE
        }
        edit_search.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                searchTxt(edit_search.text.toString())
                return@setOnEditorActionListener true
            }
            false
        }
        set = PreferenceManager.getDefaultSharedPreferences(this).getStringSet("lable", mutableSetOf())
        if (!set.isEmpty()) {
            layout_search.visibility = View.VISIBLE
        }
        adapter = object : _BaseAdapter<String>(set.toMutableList()) {
            override fun getView(p0: LayoutInflater?, s: String?, p2: Int, view: View?, viewGroup: ViewGroup): View {
                val holder = _getViewHolder(view, viewGroup, R.layout.grid_item_search_txt1)
                holder.setText(R.id.tv_name, s)
                holder.rootView.setOnClickListener {
                    searchTxt(s!!)
                }
                return holder.rootView
            }
        }
        grid_history.adapter = adapter
        hotAdapter = object : _BaseAdapter<Apperance>() {
            override fun getView(p0: LayoutInflater?, s: Apperance, position: Int, view: View?, viewGroup: ViewGroup): View {
                val holder = _getViewHolder(view, viewGroup, R.layout.grid_item_search_txt2)
                when (position % 4) {
                    0 -> {
                        holder.setBackgroundRes(R.id.tv_name, R.drawable.search_txt_bg_2)
                    }
                    1 -> {
                        holder.setBackgroundRes(R.id.tv_name, R.drawable.search_txt_bg_3)
                    }
                    2 -> {
                        holder.setBackgroundRes(R.id.tv_name, R.drawable.search_txt_bg_4)
                    }
                    3 -> {
                        holder.setBackgroundRes(R.id.tv_name, R.drawable.search_txt_bg_5)
                    }
                }
                holder.setText(R.id.tv_name, s.sign)
                holder.rootView.setOnClickListener {
                    searchTxt(s.sign)
                }
                return holder.rootView
            }
        }
        grid_hot.adapter = hotAdapter
    }

    fun searchTxt(key: String) {
        if (!key.isEmpty()) {
            if (set.add(key)) {
                layout_search.visibility = View.VISIBLE
                adapter._addItemToUpdate(key)
            }
            edit_search.setText("")
            startActivity(Intent(this, SearchResultActivity::class.java).putExtra(_EXTRA_String_KEY, key))
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        PreferenceManager.getDefaultSharedPreferences(this).edit().putStringSet("lable", set).apply()
    }
}